﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _20170124_Testwork2
{
    class Vector
    {
        private readonly int[] array;

        public Vector(int count)
        {
            array = new int[count];
            FillArray();
        }

        public int this[int index]
        {
            get
            {
                if (index >= 0 && index < array.Length)
                    return array[index];
                else
                {
                    string msg = String.Format("Массив содержит элементы {0} - {1}. Элемента {2} нет", 0, array.Length, index);
                    throw new IndexOutOfRangeException(msg);
                }
            }
            set
            {
                if (index >= 0 && index < array.Length)
                    array[index] = value;
                else
                {
                    string msg = String.Format("Массив содержит элементы {0} - {1}. Элемента {2} нет", 0, array.Length, index);
                    throw new IndexOutOfRangeException(msg);
                }

            }
        }

        public static Vector SumVector(Vector a, Vector b)
        {
            if (a.GetLength() != b.GetLength())
                throw new InvalidOperationException("Длина векторов не совпадает");
            else
            {
                int size = a.GetLength();
                Vector sum = new Vector(size);
                for (int i = 0; i < size; i++)
                    sum[i] = a[i] + b[i];
                return sum;
            }
        }
        public static Vector MinusVector(Vector a, Vector b)
        {
            if (a.GetLength() != b.GetLength())
                throw new InvalidOperationException("Длина векторов не совпадает");
            else
            {
                int size = a.GetLength();
                Vector sum = new Vector(size);
                for (int i = 0; i < size; i++)
                    sum[i] = a[i] - b[i];
                return sum;
            }
        }

        private void FillArray()
        {
            Random r = new Random(DateTime.Now.Millisecond);
            Thread.Sleep(10);
            for (int i = 0; i < array.Length; i++)
                array[i] = r.Next(21);
        }

        private int GetLength()
        {
            return array.Length;
        }

        public override string ToString()
        {
            StringBuilder str = new StringBuilder();
            foreach (var item in array)
            {
                str.Append(item + "\t");
            }
            return str.ToString();
        }

    }
}
